﻿using JESAI.Core.Modularity;
using System.ComponentModel;
using Microsoft.Extensions.DependencyInjection;
using JESAI.Core.Module;
using JESAI.Core.Extensions;
using JESAI.Application.Service;
using JESAI.Platform.Sqlsugar;
using Microsoft.Extensions.DependencyInjection.Extensions;
using JESAI.Data.Filters;
using JESAI.Datas.Sqlsugar.Filters;
using JESAI.Datas.Sqlsugar.Options;
using JESAI.Datas.Sqlsugar.Repositories;
using JESAI.DDD.Repositories;
using JESAI.Core.Configuration;
using JESAI.Platform.Domain;
using JESAI.Uow;
using JESAI.Authorize;
using JESAI.Authorization.MySql;
using JESAI.Core;
using JESAI.Framework.Http;
using NLog.Extensions.Logging;
using JESAI.Framework;
using JESAI.Authorization.SQLite;

namespace JESAI.Platform.WebApi.Modules
{
    /// <summary>
    /// AppModule模块
    /// </summary>
    [Description("AppModule模块")]
    [DependsOnModule(typeof(SqlsugarModule), 
        typeof(CachingModule),
        typeof(MapperModule),
        typeof(LogModule),typeof(MQTTModule))]
    public class AppModule:JESAIModule
    {
        /// <summary>
        /// 模块级别。级别越小越先启动
        /// </summary>
        public override ModuleLevel Level => ModuleLevel.Application;

        /// <summary>
        /// 模块启动顺序。模块启动的顺序先按级别启动，同一级别内部再按此顺序启动，
        /// </summary>
        public override int Order => 4;

        /// <summary>
        /// 添加服务。将模块服务添加到依赖注入服务容器中
        /// </summary>
        /// <param name="services">服务集合</param>
        public override IServiceCollection AddServices(IServiceCollection services)
        {

            services.AddJESAIModules(typeof(AppModule));
            services.AddJESAIModules(typeof(MainSqlsugarModule));
            services.AddJESAIModules(typeof(ApplicationServiceModule));
            services.AddJESAIModules(typeof(JESAIDomainModule));
            services.AddUow();
            services.AddSingleton<IUser, HttpContextUser>();
            services.AddAutoApiService(opt =>
            {
                //NETServiceTest所在程序集添加进动态api配置
                opt.CreateConventional(ModuleAssembly.Assemblies, option => option.RootPath = string.Empty);
            });
            var configuration = services.GetConfiguration();
            services.AddTokenJwtAuthorize(configuration);
            if (configuration["JwtAuthorize:DBType"] == "MySql")
            {
                services.AddApiJwtAuthorize().UseAuthoriser(builder => { builder.UseMySqlAuthorize(); });
            }

            if (configuration["JwtAuthorize:DBType"] == "SQLite")
            {
                services.AddApiJwtAuthorize().UseAuthoriser(builder => { builder.UseSQLiteAuthorize(); });
            }
           
            

            //全盘扫描,自动依赖注入
            services.AddAutoIocServer();
       
            //services.AddLogging(loggingBuilder =>
            //{
            //    loggingBuilder.AddNLog();
            //});
            return services;
        }

        /// <summary>
        /// 应用模块服务
        /// </summary>
        /// <param name="provider">服务提供程序</param>
        public override void UseModule(IServiceProvider provider)
        {          
            Enabled = true;
        }

        public override void UseModule(IApplicationBuilder app)
        {
            app.UseJESAIFramework();
        }
    }
}
